package com.panshi.service.impl;

import com.panshi.mapper.IMessagMapper;
import com.panshi.service.IMessagService;
import com.panshi.vo.qa.DataResultQA;
import com.panshi.vo.qa.Messag;
import com.panshi.vo.qa.Pages;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class MessagService implements IMessagService {

    @Autowired
    IMessagMapper messagMapper;

    @Override
    @Transactional(propagation= Propagation.REQUIRED)//spring事务注解型
    public DataResultQA addMessag(Messag messag) {
        DataResultQA dataResultQA = new DataResultQA();
        try {
            if(messag.getContent() == null && messag.getUserId() <= 0){
                dataResultQA.setBoo(false);
                dataResultQA.setCode(110);
                dataResultQA.setDescribe("参数不正确！");
                return dataResultQA;
            }

            messagMapper.addMessag(messag);

            dataResultQA.setBoo(true);
            dataResultQA.setCode(200);
            dataResultQA.setDescribe("添加完成！");
            return dataResultQA;
        }catch (Exception e){
            dataResultQA.setBoo(false);
            dataResultQA.setCode(120);
            dataResultQA.setDescribe("添加失败！");
            return dataResultQA;
        }
    }

    @Override
    public DataResultQA<List<Messag>> inquirePagesMessag(Pages pages) {
        DataResultQA<List<Messag>> dataResultQA = new DataResultQA();
        if(pages.getStartPage() < 0 || pages.getPageSize() <= 0){
            dataResultQA.setCode(110);
            dataResultQA.setBoo(false);
            dataResultQA.setDescribe("参数不正确！");
            return dataResultQA;
        }
        List<Messag> list = messagMapper.inquirePagesMessag(pages);
        dataResultQA.setCode(200);
        dataResultQA.setBoo(true);
        dataResultQA.setDescribe("分页查询评论完成！");
        dataResultQA.setData(list);
        return dataResultQA;
    }

    @Override
    //@CacheEvict(value = "messag:inquireSum",key = "#userId")
    public DataResultQA<Integer> inquireSum(int userId) {
        DataResultQA<Integer> dataResultQA = new DataResultQA();
        if(userId <= 0){
            dataResultQA.setCode(110);
            dataResultQA.setBoo(false);
            dataResultQA.setDescribe("参数不正确！");
            return dataResultQA;
        }

        Integer sum = messagMapper.inquireSum(userId);

        dataResultQA.setCode(200);
        dataResultQA.setBoo(true);
        dataResultQA.setDescribe("查询未读站内消息完成！");
        dataResultQA.setData(sum);
        return dataResultQA;
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED)//spring事务注解型
    public DataResultQA updateMessag(Messag messag) {
        DataResultQA dataResultQA = new DataResultQA();
        try {
            boolean boo = messag.getCheckDescribe() == null || messag.getUserId() <= 0;
            if(boo){
                dataResultQA.setCode(110);
                dataResultQA.setBoo(false);
                dataResultQA.setDescribe("参数不正确！");
                return dataResultQA;
            }
            messagMapper.updateMessag(messag);

            dataResultQA.setBoo(true);
            dataResultQA.setCode(200);
            dataResultQA.setDescribe("修改完成！");
            return dataResultQA;
        }catch (Exception e){
            dataResultQA.setBoo(false);
            dataResultQA.setCode(120);
            dataResultQA.setDescribe("修改失败！");
            return dataResultQA;
        }
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED)//spring事务注解型
    public DataResultQA messagMsgRead(Integer id) {
        DataResultQA dataResultQA = new DataResultQA();
        try {
            if(id <= 0){
                dataResultQA.setCode(110);
                dataResultQA.setBoo(false);
                dataResultQA.setDescribe("参数不正确！");
                return dataResultQA;
            }
            messagMapper.messagMsgRead(id);

            dataResultQA.setBoo(true);
            dataResultQA.setCode(200);
            dataResultQA.setDescribe("修改已读完成！");
            return dataResultQA;
        }catch (Exception e){
            dataResultQA.setBoo(false);
            dataResultQA.setCode(120);
            dataResultQA.setDescribe("修改已读失败！");
            return dataResultQA;
        }
    }
}
